package com.jdd.fintech.morse.plugin.strategy.context;

import com.alibaba.fastjson.JSONObject;
import com.jdd.fintech.morse.plugin.ecrypts.SymmetricCrypto;
import com.jdd.fintech.morse.plugin.enums.BizErrorCodeEnum;
import com.jdd.fintech.morse.plugin.enums.CustomVersionEnum;
import com.jdd.fintech.morse.plugin.plugconfig.MorsePlugValueConfig;
import com.jdd.fintech.morse.plugin.strategy.*;
import com.jddglobal.filter.support.exception.FilterException;
import com.jddglobal.phevos.gateway.resources.ResourceCapability;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

/**
 * @description: 数据加密策略引用类
 * @author: fengcheng
 * @credit: 2021-12-24 11:05
 **/
public class DataEncryptAndDecryptStrategyContext {

    /**
     * 数据加密策略类
     */
    private AbstractDataEncryptAndDecryptStrategy abstractDataEncryptAndDecryptStrategy;

    /**
     * 日志输出对象
     */
    private static final Logger log = LoggerFactory.getLogger(DataEncryptAndDecryptStrategyContext.class);

    /**
     * 根据定制化版本获取对应的策略类
     *
     * @param customVersionEnum 定制化版本枚举
     * @throws FilterException
     */
    public DataEncryptAndDecryptStrategyContext(CustomVersionEnum customVersionEnum) throws FilterException {
        if (ObjectUtils.isEmpty(customVersionEnum)) {
            log.error("DataEncryptAndDecryptStrategyContext.DataEncryptAndDecryptStrategyContext|初始化策略类|定制化版本不允许为空:{}", customVersionEnum.getCode());
            throw new FilterException(new ResourceCapability.AutoDefineResource(BizErrorCodeEnum.FAILURE_0.getCode(), BizErrorCodeEnum.FAILURE_0.getDesc()));
        }
        switch (customVersionEnum) {
            case G: //表示为：金条合作方
                this.abstractDataEncryptAndDecryptStrategy = new JtDataEncryptAndDecryptStrategy();
                break;
            case JC: //表示为：金城合作方
                this.abstractDataEncryptAndDecryptStrategy = new JcDataEncryptAndDecryptStrategy();
                break;
            case MS: //表示为：Morse标准化
                this.abstractDataEncryptAndDecryptStrategy = new MsDataEncryptAndDecryptStrategy();
                break;
            case CL: //表示为：云地结合
                this.abstractDataEncryptAndDecryptStrategy = new ClDataEncryptAndDecryptStrategy();
                break;
            case PAPH: //表示为：平安惠普
                this.abstractDataEncryptAndDecryptStrategy = new PahpDataEncryptAndDecryptStrategy();
                break;
            case YGC: //表示为：云工厂
                this.abstractDataEncryptAndDecryptStrategy = new YgcDataEncryptAndDecryptStrategy();
                break;
            default:
                throw new FilterException(new ResourceCapability.AutoDefineResource(BizErrorCodeEnum.FAILURE_1.getCode(), BizErrorCodeEnum.FAILURE_1.getDesc()));
        }
    }

    /**
     * 通过策略类调用数据加密方法
     * @param plugValueConfig
     * @param jsonObject
     * @param scObj
     * @return
     * @throws FilterException
     */
    public String dataEncryptsStrategyMethod(MorsePlugValueConfig plugValueConfig, JSONObject jsonObject, SymmetricCrypto scObj) throws FilterException {
        log.info("DataEncryptAndDecryptStrategyContext.dataEncryptsStrategyMethod.开始调用策略类执行具体加密接口");
        return abstractDataEncryptAndDecryptStrategy.dataEncryptsStrategyMethod(plugValueConfig, jsonObject, scObj);
    }

    /**
     * 通过策略类调用数据解密方法
     * @param plugValueConfig
     * @param jsonObject
     * @param scObj
     * @return
     * @throws FilterException
     */
    public String dataDecryptsStrategyMethod(MorsePlugValueConfig plugValueConfig, JSONObject jsonObject, SymmetricCrypto scObj) throws FilterException {
        log.info("DataEncryptAndDecryptStrategyContext.dataDecryptsStrategyMethod.开始调用策略类执行具体解密接口");
        return abstractDataEncryptAndDecryptStrategy.dataDecryptsStrategyMethod(plugValueConfig, jsonObject, scObj);
    }

    /**
     * 通过策略类调用数据构建公共请求对象方法
     * @param plugValueConfig
     * @param tenantId
     * @param productCode
     * @param jsonObject
     * @return
     * @throws FilterException
     */
    public String buildCommonRequestObjMethod(MorsePlugValueConfig plugValueConfig, String tenantId, String productCode, JSONObject jsonObject) throws FilterException {
        log.info("DataEncryptAndDecryptStrategyContext.buildCommonRequestObjMethod.开始调用策略类执行具体构建响应对象接口");
        return abstractDataEncryptAndDecryptStrategy.buildCommonRequestObjMethod(plugValueConfig, tenantId, productCode, jsonObject);
    }

}
